package com.yenlien.traceyun.business.interceptor;

import com.yenlien.traceyun.common.crypt.TokenUtil;
import com.yenlien.traceyun.common.exception.TokenException;
import com.yenlien.traceyun.common.utils.LoggerUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-10-22
 * @description:
 **/
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //通过注解拦截，如果不是映射到方法直接通过
        if ((handler instanceof HandlerMethod)) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Class clazz = handlerMethod.getBean().getClass();
            LoggerUtil.logInfo(clazz.getSimpleName());
            Method method = handlerMethod.getMethod();
            //检查是否有passtoken注释，有则跳过认证
            if (method.isAnnotationPresent(DataAuth.class)) {
                DataAuth passToken = method.getAnnotation(DataAuth.class);
                request.setAttribute("auth", passToken.required());
            }
        }
        return true;
    }
}
